// pages/record/record.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
		recordList: [
		],
		report_img: ''
  },

	// 统计数据的事件
	logvisit: function () {
		wx.request({
			url: app.globalData.baseurl + '/API/skin/log_visit',
			method: 'GET',
			data: {
				openid: this.data.id,
				pageid: 3,
				content: ''
			},
			success: function (res) {
				console.log(res)
			}
		})
	},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		var _this = this
		wx.setNavigationBarTitle({
			title: '肌肤档案'
		})
		wx.getStorage({
			key: "userinfo",
			success: function (res) {
				wx.request({
          url: app.globalData.baseurl + '/api/fans/showskin?openid=' + res.data.openid,
					method: 'Get',
					success: function (res) {
						console.log(res)
						_this.setRecordList(res)
					}
				})
				_this.setData({
					id: res.data.openid
				})
				_this.logvisit()
			}
		})
  },

	setRecordList: function (res) {
		var _this = this
		if (res.data !== '') {
			res.data.forEach(function (item) {
				item.report_img = app.globalData.baseurl + item.report_img
				item.report_time = _this.timeChange(item.report_time)
				console.log(_this.timeChange(item.report_time))
			})
		}
		this.setData({
			recordList: res.data
		})
	},
	// 点击拍照
	takephoto: function () {
		var that = this
		wx.chooseImage({
			count: 1, // 默认9
			sizeType: 'compressed', // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: function (res) {
				// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
				var tempFilePaths = res.tempFilePaths
				wx.uploadFile({
					url: app.globalData.baseurl + '/api/uploads/upload/name/img', //仅为示例，非真实的接口地址
					filePath: tempFilePaths[0],
					name: 'img',
					success: function (res) {
						console.log(JSON.parse(res.data))
						var data = JSON.parse(res.data)
						that.setData({
							report_img: data.url
						})
						that.uploadTakePhotoMsg(res)
						//do something
					}
				})
			}
		})
	},

	uploadTakePhotoMsg() {
		var id = ''
		var that = this
		var timestamp = Date.parse(new Date()) / 1000;
		var data = {
			report_time: timestamp,
			openid: that.data.id,
			report_img: that.data.report_img
		}
		wx.showToast({
			title: '上传中！',
			icon: 'loading',
			duration: 2000
		})
		wx.request({
			url: app.globalData.baseurl + '/api/fans/faceonly',
			method: 'POST',
			data: {
				image: app.globalData.baseurl + that.data.report_img,
				imagetype: 'URL'
			},
			success: function (res) {
				console.log(res)
				if (res.data.error_code === 0) {
					wx.request({
						url: app.globalData.baseurl + '/api/user/saveReport',
						method: 'POST',
						data: data,
						success: function (res) {
							console.log(res)
							wx.showToast({
								title: '提交成功！',
								icon: 'success',
								duration: 1000
							})
							// 将提交的信息保存到缓存中
							try {
								wx.setStorageSync('report', that.data.report_img)
							} catch (e) { }
							setTimeout(function () {
								wx.navigateTo({
									url: '../sure/sure'
								})
							}, 1000)
						}
					})
				} else {
					wx.request({
						url: app.globalData.baseurl + '/api/user/saveReport',
						method: 'POST',
						data: data,
						success: function (res) {
							console.log(res)
						}
					})
					wx.showToast({
						title: '提交失败或未识别到人脸，请重新拍照！',
						icon: 'none',
						duration: 2000
					})
				}
			}
		})
	},

	timeChange: function (timestamp) {
		var date = new Date(timestamp * 1000) // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
		var Y = date.getFullYear() + '-'
		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
		var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
		var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
		var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
		var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
		return Y + M + D + h + m + s
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})